package org.cbg.panel;

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.swing.*;

import org.cbg.bean.GoodsBackBean;
import org.cbg.btn.TrslationBtn;
import org.cbg.entity.Roleorder;
import org.cbg.frame.TrslationMainJframe;
import org.cbg.mouslisten.CBGmoveMouslisten;
import org.come.Frame.ZhuFrame;
import org.come.socket.Agreement;
import org.come.socket.SendMessageUntil;
import org.come.until.CutButtonImage;
import org.come.until.GoodsListFromServerUntil;
import org.come.until.GsonUtil;

import com.tool.tcpimg.UIUtils;



/**
 * 藏宝阁 我的藏宝阁 我的货物
 * 
 * @author Administrator
 * 
 */

public class TraslationMyMainMygoodsModelJpanel extends JPanel {

    // 展示底框 展示图标 名字 等级 快速取回 ,多选框 ， 勾选
    private JLabel zhanshidikuang, zhangshi, mingzi, dengji, quanxuankuang, gouxuan;
    public TrslationBtn kuaisuquhui;

    public TraslationMyMainMygoodsModelJpanel() {
        // TODO Auto-generated constructor stub

        /**
         * 面板设置
         */
        this.setPreferredSize(new Dimension(304, 50));
        this.setOpaque(false);
        this.setLayout(null);


        /**
         * 初始化组件
         */
        zhanshidikuang = new JLabel();
        zhangshi = new JLabel();
        mingzi = new JLabel("名字");
        gouxuan = new JLabel();
        quanxuankuang = new JLabel();
        dengji = new JLabel("120");
        kuaisuquhui = new TrslationBtn("inkImg/number/b4/ksqhaj.png", 1, UIUtils.COLOR_BTNTEXT, UIUtils.TEXT_FONT, "");
        /**
         * 边框透明化
         */
        zhanshidikuang.setBorder(null);
        zhangshi.setBorder(null);
        mingzi.setBorder(null);
        dengji.setBorder(null);
        kuaisuquhui.setBorder(null);

        /**
         * 设置组件的大小和位置
         */
        zhanshidikuang.setBounds(38, 2, 53, 53);
        zhangshi.setBounds(40, 5, 39, 39);
        mingzi.setBounds(190, 5, 100, 39);
        gouxuan.setBounds(13, 20, 17, 16);
        quanxuankuang.setBounds(13, 20, 17, 16);
        dengji.setBounds(353, 5, 39, 39);
        kuaisuquhui.setBounds(630, 15, 95, 26);

        /**
         * 组件添加背景
         */
        zhanshidikuang.setIcon(CutButtonImage.getImage("inkImg/number/b4/spbj.png", 53, 53));
        zhangshi.setIcon(CutButtonImage.getImage("inkImg/hongmu/2/217.png", 39, 39));
        gouxuan.setIcon(null);
        quanxuankuang.setIcon(CutButtonImage.getImage("inkImg/number/b4/gxk.png", 17, 16));

        /**
         * 组件透明化
         */
        zhanshidikuang.setOpaque(false);
        zhangshi.setOpaque(false);
        mingzi.setOpaque(false);
        gouxuan.setOpaque(false);
        quanxuankuang.setOpaque(false);
        dengji.setOpaque(false);
        kuaisuquhui.setOpaque(false);

        /**
         * 修改组件名字
         */
        zhanshidikuang.setName("1");
        gouxuan.setName("1");

        /**
         * 组件字体设置颜色
         */
        mingzi.setForeground(UIUtils.COLOR_cbg6);
        dengji.setForeground(UIUtils.COLOR_cbg6);

        /**
         * 面板添加组件
         */
        this.add(zhanshidikuang);
        this.add(zhangshi);
        this.add(mingzi);
        this.add(gouxuan);
        this.add(quanxuankuang);
        this.add(dengji);
        this.add(kuaisuquhui);

        /**
         * 组件添加监听
         */
        gouxuan.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                TraslationMyMainCardJpanel traslationMyMainCardJpanel = TrslationMainJframe.getTrslationMainJframe().getTrslationMainJpanel().getTranslationMainCardJpanel().getTraslationMyMainJpanel().getTraslationMyMainCardJpanel();
                if ("1".equals(gouxuan.getName())) {
                    gouxuan.setIcon(CutButtonImage.getImage("skin/8100226/button/13.png", 15, 15));
                    gouxuan.setName("0");
                    TrslationMainJframe.getTrslationMainJframe().setGoodsGouxuangeshu(
                            TrslationMainJframe.getTrslationMainJframe().getGoodsGouxuangeshu() + 1);
                    if (TrslationMainJframe.getTrslationMainJframe().getGoodsGouxuangeshu() == 10) {
                        traslationMyMainCardJpanel.getTraslationMyMainMygoodsJpanel().getGouxuan()
                                .setIcon(CutButtonImage.getImage("skin/8100226/button/13.png", 15, 15));
                        traslationMyMainCardJpanel.getTraslationMyMainMygoodsJpanel().getGouxuan().setName("0");
                    }
                } else {
                    gouxuan.setIcon(null);
                    gouxuan.setName("1");
                    TrslationMainJframe.getTrslationMainJframe().setGoodsGouxuangeshu(
                            TrslationMainJframe.getTrslationMainJframe().getGoodsGouxuangeshu() - 1);
                    if (TrslationMainJframe.getTrslationMainJframe().getGoodsGouxuangeshu() != 10) {
                        traslationMyMainCardJpanel.getTraslationMyMainMygoodsJpanel().getGouxuan().setIcon(null);
                        traslationMyMainCardJpanel.getTraslationMyMainMygoodsJpanel().getGouxuan().setName("1");
                    }
                }

            }
        });

    }

    /**
     * 添加数据到面板上 salegoods.getBuyrole()//salegoods.getContiontype()//
     * salegoods.getOtherid
     * ()//salegoods.getRoleid()//salegoods.getSaleid()//salegoods.getSaletype()
     */
    public TraslationMyMainMygoodsModelJpanel(Roleorder roleorder) {
        /**
         * 面板设置
         */
        this.setPreferredSize(new Dimension(304, 50));
        this.setOpaque(false);
        this.setLayout(null);

        /**
         * 组件初始化
         */
        zhanshidikuang = new JLabel();
        zhangshi = new JLabel();
        mingzi = new JLabel(roleorder.getSalename());
        gouxuan = new JLabel();
        quanxuankuang = new JLabel();
        dengji = new JLabel();
        kuaisuquhui = new TrslationBtn("inkImg/number/b4/ksqhaj.png", 1, UIUtils.COLOR_BTNTEXT, UIUtils.TEXT_FONT, "");

        /**
         * 组件边框透明化
         */
        zhanshidikuang.setBorder(null);
        zhangshi.setBorder(null);
        mingzi.setBorder(null);
        dengji.setBorder(null);
        kuaisuquhui.setBorder(null);
        mingzi.setFont(UIUtils.TEXT_FONT2);
        mingzi.setForeground(UIUtils.COLOR_cbg6);
        dengji.setFont(UIUtils.TEXT_FONT2);
        dengji.setForeground(UIUtils.COLOR_cbg6);
        /**
         * 设置组件的大小和位置
         */
        zhanshidikuang.setBounds(38, 2, 53, 53);
        zhangshi.setBounds(40, 5, 48, 48);
        mingzi.setBounds(100, 5, 135, 39);
        gouxuan.setBounds(13, 20, 17, 16);
        quanxuankuang.setBounds(13, 20, 17, 16);
        dengji.setBounds(353, 5, 39, 39);
        kuaisuquhui.setBounds(630, 15, 95, 26);

        /**
         * 组件设置图片
         */
        zhanshidikuang.setIcon(CutButtonImage.getImage("inkImg/number/b4/spbj.png", 53, 53));
        zhangshi.setIcon(CutButtonImage.getCBG(roleorder.getSaletype(), roleorder.getSaleskin(), 48, 48));
        gouxuan.setIcon(null);
        quanxuankuang.setIcon(CutButtonImage.getImage("inkImg/number/b4/gxk.png", 17, 16));

        /**
         * 组件透明化
         */
        zhanshidikuang.setOpaque(false);
        zhangshi.setOpaque(false);
        mingzi.setOpaque(false);
        gouxuan.setOpaque(false);
        quanxuankuang.setOpaque(false);
        dengji.setOpaque(false);
        kuaisuquhui.setOpaque(false);

        /**
         * 组件设置名字
         */
        zhanshidikuang.setName(roleorder.getSaleid().toString());
        gouxuan.setName("1");

        /**
         * 组件设置字体颜色
         */
        mingzi.setForeground(UIUtils.COLOR_cbg6);
        dengji.setForeground(UIUtils.COLOR_cbg6);

        /**
         * 组件输入内容（1未付钱 2超时 3已付钱 4已取货）
         */
        dengji.setText(setBack(roleorder.getStatus()));
        if (roleorder.getStatus() != 6) {
            kuaisuquhui.setVisible(false);
            quanxuankuang.setVisible(false);
            gouxuan.setVisible(false);
        } else {
            kuaisuquhui.setVisible(true);
            quanxuankuang.setVisible(true);
            gouxuan.setVisible(true);
        }

        /**
         * 面板添加图片
         */
        this.add(zhanshidikuang);
        this.add(zhangshi);
        this.add(mingzi);
        this.add(gouxuan);
        this.add(quanxuankuang);
        this.add(dengji);
        this.add(kuaisuquhui);

        /**
         * 组件添加监听
         */
        gouxuan.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                TraslationMyMainCardJpanel traslationMyMainCardJpanel = TrslationMainJframe.getTrslationMainJframe().getTrslationMainJpanel().getTranslationMainCardJpanel().getTraslationMyMainJpanel().getTraslationMyMainCardJpanel();
                if ("1".equals(gouxuan.getName())) {
                    gouxuan.setIcon(CutButtonImage.getImage("skin/8100226/button/13.png", 15, 15));
                    gouxuan.setName("0");
                    TrslationMainJframe.getTrslationMainJframe().setGoodsGouxuangeshu(
                            TrslationMainJframe.getTrslationMainJframe().getGoodsGouxuangeshu() + 1);
                    if (TrslationMainJframe.getTrslationMainJframe().getGoodsGouxuangeshu() == TrslationMainJframe
                            .getTrslationMainJframe().getGoodsGeshuMax()) {
                        traslationMyMainCardJpanel.getTraslationMyMainMygoodsJpanel().getGouxuan()
                                .setIcon(CutButtonImage.getImage("skin/8100226/button/13.png", 15, 15));
                        traslationMyMainCardJpanel.getTraslationMyMainMygoodsJpanel().getGouxuan().setName("0");
                    }
                } else {
                    gouxuan.setIcon(null);
                    gouxuan.setName("1");
                    TrslationMainJframe.getTrslationMainJframe().setGoodsGouxuangeshu(
                            TrslationMainJframe.getTrslationMainJframe().getGoodsGouxuangeshu() - 1);
                    if (TrslationMainJframe.getTrslationMainJframe().getGoodsGouxuangeshu() != TrslationMainJframe
                            .getTrslationMainJframe().getGoodsGeshuMax()) {
                        traslationMyMainCardJpanel.getTraslationMyMainMygoodsJpanel().getGouxuan().setIcon(null);
                        traslationMyMainCardJpanel.getTraslationMyMainMygoodsJpanel().getGouxuan().setName("1");
                    }
                }
            }
        });
        kuaisuquhui.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                // TODO Auto-generated method stub

                // 返回背包剩余格数
                int packNumber = GoodsListFromServerUntil.Surplussum("-1", "-1", 999);
                // 判断背包格数是否剩余
                if (packNumber > 0) {
                    /**
                     * 快速取回发送给服务器
                     */
                    GoodsBackBean backBean = new GoodsBackBean();
                    List<BigDecimal> mesIdList = new ArrayList<>();
                    mesIdList.add(new BigDecimal(zhanshidikuang.getName()));

                    backBean.setIds(mesIdList);
                    /**
                     * 转换为对应协议的json字符串 提交给服务器处理
                     */
                    String sendmes = Agreement.getAgreement().searchGoodsBackAgreement(
                            GsonUtil.getGsonUtil().getgson().toJson(backBean));
                    SendMessageUntil.toServer(sendmes);

                    if ("0".equals(gouxuan.getName())) {
                        TrslationMainJframe.getTrslationMainJframe().setGoodsGouxuangeshu(
                                TrslationMainJframe.getTrslationMainJframe().getGoodsGouxuangeshu() - 1);
                    }
                    TrslationMainJframe.getTrslationMainJframe().setGoodsGeshuMax(
                            TrslationMainJframe.getTrslationMainJframe().getGoodsGeshuMax() - 1);
                    gouxuan.setName("1");
                    gouxuan.setIcon(null);
                    gouxuan.setVisible(false);
                    kuaisuquhui.setVisible(false);
                    quanxuankuang.setVisible(false);
                } else {
                    ZhuFrame.getZhuJpanel().addPrompt2("背包格数不足");
                }
            }
        });

        zhangshi.addMouseListener(new CBGmoveMouslisten(roleorder.getSaletype(), roleorder.getOtherid()));
    }

    public String setBack(Integer status) {
        kuaisuquhui.setVisible(false);
        String dengji = null;

        switch (status) {
            case 1:
                dengji = "未付钱";
                break;
            case 2:
                dengji = "超时";
                break;
            case 3:
                dengji = "已付钱";

                break;
            case 4:
                dengji = "已取货";
                break;
            case 5:
                dengji = "已发货";

                break;
            case 6:
                dengji = "待取回";
                kuaisuquhui.setVisible(true);
                break;
            case 7:
                dengji = "已取回";

                break;
            default:
                break;
        }
        return dengji;
    }

    private ImageIcon icon;

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (icon == null) {
            icon = CutButtonImage.getImage("inkImg/old/1/Z1001.png", 740, 2);
        }
        g.drawImage(icon.getImage(), 0, 58, 740, 2, this);


    }

}
